# -*- coding:utf-8 -*-
# 正则表达式

import re;

string = 'this is a string'
patt = 'str'
res = re.search(patt, string)
print(res) # <_sre.SRE_Match object; span=(10, 13), match='str'>

string2 = 'this is string2'
patt2 = '\w\d'
res2 = re.search(patt2, string2) # <_sre.SRE_Match object; span=(13, 15), match='g2'>
print(res2)

string3 = 'this is string3'
patt3 = 'str[ing]ng'
patt4 = 'str[ing]g'
print(re.search(patt3, string3)) # <_sre.SRE_Match object; span=(8, 14), match='string'>
print(re.search(patt4, string3)) # None

string5 = 'this is string5'
print(re.search('.str..', string5)) # <_sre.SRE_Match object; span=(7, 13), match=' strin'>

string6 = 'php and python'
print(re.search('python|php', string6)) # <_sre.SRE_Match object; span=(0, 3), match='php'>

string7 = 'PHP and python'
print(re.search('php', string7))
print(re.search('php', string7, re.I))

string8 = 'php and python'
print(re.search('p.*h', string8)) # <_sre.SRE_Match object; span=(0, 12), match='php and pyth'>
print(re.search('p.*?h', string8)) # <_sre.SRE_Match object; span=(0, 2), match='ph'>

string9 = 'js php and python'
string10 = 'php and python'
print(re.match('p.*h', string9)) # None
print(re.match('p.*h', string10)) # <_sre.SRE_Match object; span=(0, 12), match='php and pyth'>

string11 = 'php and python'
print(re.compile('p.*?h').findall(string11)) # ['ph', 'p and pyth']

# 匹配网址(以下规则不对)
patt12 = '[a-zA-Z]+://\S+[\.com|\.cn]+'
patt12 = '[a-zA-Z]+://\S+[\.com|\.cn]+'
string12 = '<a href="https://www.baidu.com"></a>'
print(re.search(patt12, string12)) # <_sre.SRE_Match object; span=(9, 30), match='https://www.baidu.com'>
